<?php
//sleep(5);
     
    include_once('connection/connection.php');
    if(isset($_REQUEST['load_circle'])) {
	//sleep(4);
		$query = mysql_query("SELECT circle_id , circle_name FROM tbl_lookup_circle WHERE zone_id='$_REQUEST[load_circle]' AND status = '1' ");
		echo "<option value='0'>select circle: </option>";
		while($circle = mysql_fetch_assoc($query)){
		echo "<option value='".$circle['circle_id']."'>".$circle['circle_name']."</option>";
		}
		unset($query);
	
	}
	if(isset($_REQUEST['add_div'])) {
	
	mysql_query("INSERT INTO tbl_lookup_division VALUES('','$_REQUEST[add_div]','$_REQUEST[circle_id]',(SELECT circle_name FROM tbl_lookup_circle WHERE circle_id=$_REQUEST[circle_id]),'$_REQUEST[zone_id]',(SELECT zone_name FROM tbl_lookup_zone WHERE zone_id=$_REQUEST[zone_id]),'1') ");
	$last_id = mysql_insert_id();
	$query = mysql_query("SELECT * FROM tbl_lookup_division WHERE div_id='$last_id'");
		$div = mysql_fetch_assoc($query);
		echo "<tr onMouseOver=\" this.style.backgroundColor='#E5E5E5'; \" onMouseOut= \"this.style.backgroundColor='#FFFFFF'; \" id= 'row_id_".$div['div_id']."' class='div_row'><td height='27' width='100'  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$div['zone_name']."</font></td>";
		echo "<td height='27' width='100'  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$div['circle_name']."</font></td>";
		echo "<td align='center'  id='div_id_".$div['div_id']."' ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$div['div_name']." </font></td>";
		echo "<td align='center' ><a href='Javascript:void(0);' onClick= \"edit_div('".$div['div_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Edit</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		echo "<span id='state_id_".$div['div_id']."' ><a href='Javascript:void(0);' onClick= \" status_handler(0,'".$div['div_id']."'); \"  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Disable</font></a></span>";

	unset($last_id);
	unset($query);
	unset($div);

		
	}
	
	if(isset($_REQUEST['status'])) {

			if(mysql_query("UPDATE tbl_lookup_division SET status = '$_REQUEST[status]' WHERE div_id='$_REQUEST[id]' ")) {
					if($_REQUEST['status']==1) echo "<a href='Javascript:void(0);' onClick=\"status_handler(0,'".$_REQUEST['id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Disable</font></a>";
					else echo "<a href='Javascript:void(0);' onClick=\"status_handler(1,'".$_REQUEST['id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Enable</font></a>";
			}
			else{
			echo mysql_error();
			}
	}
	
	
	if(isset($_REQUEST['edit_id'])){
	//print_r($_REQUEST);
	
	$query = mysql_query("SELECT * FROM tbl_lookup_division WHERE div_id = '$_REQUEST[edit_id]' ");
	$detail = mysql_fetch_assoc($query);
	echo "<td >";
	echo "<select id='zone_id' onChange= \" load_circle(this.value, 'circle_id'); \" >";
	$query = mysql_query("SELECT * FROM tbl_lookup_zone WHERE status = 1  ORDER BY zone_id desc");
	echo "<option value='0'>select zone:</option>";
		while($zone = mysql_fetch_assoc($query)){
			if($zone['zone_id'] == $detail['zone_id']) {
			echo "<option value='".$zone['zone_id']."' selected>".$zone['zone_name']."</option>";
			}
			else{
			echo "<option value='".$zone['zone_id']."'>".$zone['zone_name']."</option>";
			}
		}
	echo "</select></td>";
	
	echo "<td >";
	echo "<select id='circle_id' >";
	$query = mysql_query("SELECT * FROM tbl_lookup_circle WHERE zone_id= '$detail[zone_id]' AND status=1 ORDER BY circle_id desc");
		echo "<option value='0'>select circle:</option>";
		while($circle = mysql_fetch_assoc($query)){
			if($circle['circle_id'] == $detail['circle_id']) {
			echo "<option value='".$circle['circle_id']."' selected>".$circle['circle_name']."</option>";
			}
			else{
			echo "<option value='".$circle['circle_id']."'>".$circle['circle_name']."</option>";
			}
		}
	echo "</select></td>";
	echo "<td align='center'><input type='text' id='division_name' value='".$detail['div_name']."' size='15'  /></td>";
	echo "<td align='center' ><a href='Javascript:void(0);' onClick= \" save_changes('".$_REQUEST['edit_id']."','division_name','circle_id','zone_id');  \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Save</font></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='Javascript:void(0);' onClick= \" default_row('".$_REQUEST['edit_id']."');  \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Cancel</font></a> </td>";
	}
	
	if(isset($_REQUEST['new_div'])) {
	//print_r($_REQUEST);
		
		mysql_query("UPDATE tbl_lookup_division SET div_name = '$_REQUEST[new_div]' , circle_id='$_REQUEST[new_circle]' , circle_name = (SELECT circle_name FROM tbl_lookup_circle WHERE circle_id= $_REQUEST[new_circle]) , zone_id = '$_REQUEST[new_zone]' , zone_name = (SELECT zone_name FROM tbl_lookup_zone WHERE zone_id= $_REQUEST[new_zone]) WHERE div_id = '$_REQUEST[id]'");
		mysql_query("UPDATE tbl_lookup_sub_division SET div_name = '$_REQUEST[new_div]' , circle_id='$_REQUEST[new_circle]' , circle_name = (SELECT circle_name FROM tbl_lookup_circle WHERE circle_id= $_REQUEST[new_circle]) , zone_id = '$_REQUEST[new_zone]' , zone_name = (SELECT zone_name FROM tbl_lookup_zone WHERE zone_id= $_REQUEST[new_zone]) WHERE div_id = '$_REQUEST[id]'");
		$query = mysql_query("SELECT * FROM tbl_lookup_division WHERE div_id='$_REQUEST[id]' ");
		$detail = mysql_fetch_assoc($query);
		echo "<td height='27' width='100' ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$detail['zone_name']."</font></td>";
		echo "<td height='27' width='100' ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$detail['circle_name']."</font></td>";
		echo "<td align='center' id='div_id_". $detail['div_id'] ."'><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$detail['div_name']."</font></td>";
		echo "<td align='center' ><a href='Javascript:void(0);' onClick=\" edit_div('".$detail['div_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Edit</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		echo "<span id='state_id_".$detail['div_id']."'>";
				if($detail['status'] == "1") echo "<a href='Javascript:void(0);' onClick=\"status_handler(0,'".$detail['div_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Disable</font></a>";
				else echo "<a href='Javascript:void(0);' onClick=\"status_handler(1,'".$detail['div_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Enable</font></a>";
		
		echo "</span></td>";
		
		unset($query);
		unset($detail);
				
		
	}
	
	
	if(isset($_REQUEST['search'])) {
	
	$query = mysql_query("SELECT DISTINCT div_id FROM tbl_lookup_division WHERE div_name like '$_REQUEST[search]%'  OR circle_name like '$_REQUEST[search]%' OR zone_name like '$_REQUEST[search]%' ");
		$sub_id = array();
		while($data = mysql_fetch_assoc($query)) {
			$sub_id[] = $data;
		}
		
		echo json_encode($sub_id);
		
	}
	
	
		if(isset($_REQUEST['load'])) {
	//echo $_REQUEST['load'];
	//echo $_REQUEST['start'];
	//sleep(1);
	$query="";
	$totol="";
	
	$current_page =($_REQUEST['start']-1)*10;
	
	if($_REQUEST['load']==""){
	$query = mysql_query("SELECT d.div_id FROM tbl_lookup_division d , tbl_lookup_circle c , tbl_lookup_zone z  WHERE d.circle_id = c.circle_id AND d.zone_id = z.zone_id AND c.status = 1 AND z.status = 1 ");
	$total = mysql_num_rows($query);
	$query = mysql_query("SELECT d.* FROM tbl_lookup_division d , tbl_lookup_circle c , tbl_lookup_zone z  WHERE d.circle_id = c.circle_id AND d.zone_id = z.zone_id AND c.status = 1 AND z.status = 1 LIMIT $current_page , 10");
	}
	else{
	$query = mysql_query("SELECT d.div_id FROM tbl_lookup_division d , tbl_lookup_circle c , tbl_lookup_zone z  WHERE d.circle_id = c.circle_id AND d.zone_id = z.zone_id AND c.status = 1 AND z.status = 1  AND (d.div_name like '$_REQUEST[load]%' OR d.circle_name like '$_REQUEST[load]%' OR d.zone_name like '$_REQUEST[load]%' ) ");
	echo mysql_error();
	$total = mysql_num_rows($query);
	$query = mysql_query("SELECT d.* FROM tbl_lookup_division d , tbl_lookup_circle c , tbl_lookup_zone z  WHERE d.circle_id = c.circle_id AND d.zone_id = z.zone_id AND c.status = 1 AND z.status = 1 AND ( d.div_name like '$_REQUEST[load]%' OR d.circle_name like '$_REQUEST[load]%' OR d.zone_name like '$_REQUEST[load]%' )  LIMIT $current_page, 10");
	}
	$total_pages = ceil($total/10);
	
		if(mysql_num_rows($query)==0) {
		echo "<tr height='50'><td colspan='4' align='center'><font style='font-size: 10pt' face='Verdana' color='red'>No Record Found</font></td></tr>";
		}
		
		while($data = mysql_fetch_assoc($query)) {
		echo "<tr onMouseOver=\" this.style.backgroundColor='#E5E5E5'; \" onMouseOut= \"this.style.backgroundColor='#FFFFFF'; \" id= 'row_id_".$data['div_id']."' class='div_row'><td height='27'  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$data['zone_name']."</font></td>";
		echo "<td><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$data['circle_name']."</font></td>";
		echo "<td height='27' align='center'  id='div_id_".$data['div_id']."'  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$data['div_name']."</font></td>";
			
		echo "<td align='center'  ><a href='Javascript:void(0);' onClick= \"edit_div('".$data['div_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Edit</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		echo "<span id='state_id_".$data['div_id']."' >";
		
		if($data['status']==1)	echo "<a href='Javascript:void(0);' onClick= \" status_handler(0,'".$data['div_id']."'); \"  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Disable</font></a>";
		else echo "<a href='Javascript:void(0);' onClick= \" status_handler(1,'".$data['div_id']."'); \"  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Enable</font></a>";
		echo "</span></td></tr>";
		
		}
		echo "<tr bgcolor='#E5E5E5' id='page_link' class='div_row'><td colspan ='4' align='center'>";
			if($_REQUEST['start']!=1) echo "<a href='javascript:void(0);' onClick= \"ajax_content('".$_REQUEST['load']."','1'); \" ><b>First</b></a>";
		for($i=1;$i<=$total_pages;$i++){
		echo "&nbsp;&nbsp;";
			
		echo "&nbsp;&nbsp;";
			if($i==$_REQUEST['start']) echo "<big>".$i."</big>";
			else echo "<a href='javascript:void(0);' onClick= \"ajax_content('".$_REQUEST['load']."','".$i."'); \" ><b>".$i."</b></a>";
		echo "&nbsp;&nbsp;";
			
		}
			if($_REQUEST['start'] < $total_pages) echo "<a href='javascript:void(0);' onClick= \"ajax_content('".$_REQUEST['load']."','".$total_pages."'); \" ><b>Last</b></a>";
		echo "</td></tr>";
		
	
	}
	
	
	
	

	
	
	
	

?>